package org.ilovecode.display {
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import org.ilovecode.buttons.RenderFloorButton;
	import org.ilovecode.buttons.TextFloorButton;
	import org.ilovecode.events.RenderEvent;
	import org.ilovecode.screen.FloorScreen;
	import org.ilovecode.screen.ScreenGroup;
	import org.ilovecode.screen.ScreenName;
	import org.ilovecode.utils.method.capitalizeFirstLetter;
	
	/**
	 * ...
	 * @author Vjekoslav Ratkajec
	 */
	public class Render extends FloorScreen 
	{
		private var _activeFloorName:String = "";
		private var _floorsApartments:Array;
		private var _floorsBusiness:Array;
		
		private const NUM_FLOORS:uint = 8;
		
		public function Render(group:String) {
			super(ScreenName.RENDER, group, "render" + capitalizeFirstLetter(group.replace("screen group ","")));
		}
		
		override protected function construct():void
		{
			super.construct();
			
			_activeFloorName = (group == ScreenGroup.APARTMENTS) ? "floor" : "floorBusiness";
			
			_floorsApartments = new Array();
			_floorsBusiness = new Array();
			
			for (var i:int = 0; i <= NUM_FLOORS; i++) 
			{
				_floorsApartments.push(BaseFloor(this.getChildByName("floor" + i)));
				_floorsBusiness.push(BaseFloor(this.getChildByName("floorBusiness" + i)));
			}
		}
		
		
		override protected function initUI():void
		{
			super.initUI();
			
			for (var i:uint = 0; i <= NUM_FLOORS; i++) {
				initTextFloorButton(i);
				BaseFloor(_floorsApartments[i]).visible = (group == ScreenGroup.APARTMENTS);
				BaseFloor(_floorsBusiness[i]).visible = (group == ScreenGroup.BUSINESS);
			}
		}
		
		override protected function addListeners():void
		{
			super.addListeners();
			
			for (var i:uint = 0; i <= NUM_FLOORS; i++) {
				this[_activeFloorName + i].addEventListener(MouseEvent.CLICK, floorClick);
			}
		}
		
		override protected function removeListeners():void
		{
			super.removeListeners();
			
			for (var i:uint = 0; i <= NUM_FLOORS; i++) {
				this[_activeFloorName + i].removeEventListener(MouseEvent.CLICK, floorClick);
			}
		}
		
		private function floorClick(event:MouseEvent):void {
			var floorName:String = event.currentTarget.name;
			var floorNumber:int = int(floorName.replace(_activeFloorName, ""));
			//var isBusiness:Boolean = (group == ScreenGroup.BUSINESS) ? true : false;
			//this.dispatchEvent(new RenderEvent(RenderEvent.FLOOR_CLICKED, floorNumber, isBusiness));
			showFloor(floorNumber);
		}
		
		override protected function initTextFloorButton(floorNumber:int):void
		{
			var buttonName:String = "floor" + ((floorNumber >= 0) ? "" : "Minus") + Math.abs(floorNumber)  + "Button";
			var refMC:MovieClip = MovieClip(this.getChildByName(buttonName + "Ref"));
			var tfb:RenderFloorButton = new RenderFloorButton(floorNumber, buttonName);
			tfb.x = refMC.x;
			tfb.y = refMC.y;
			this.addChild(tfb);
			_floorButtons.push(tfb);
		}
		
		override protected function floorButton_MouseOver(event:MouseEvent):void {
			var floorNumber:int = TextFloorButton(event.currentTarget).id;
			BaseFloor(this[_activeFloorName + floorNumber]).show();
		}
		
		override protected function floorButton_MouseOut(event:MouseEvent):void {
			var floorNumber:int = TextFloorButton(event.currentTarget).id;
			BaseFloor(this[_activeFloorName + floorNumber]).hide();
		}
		
		override public function showFloor(floorNumber:int):void {
			//this.dispatchEvent(new RenderEvent(RenderEvent.FLOOR_CLICKED, floorNumber));
			controller.showFloor(floorNumber);
		}
		
	}

}